home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo3.zoo / demo / misc / mgrmsgs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  5.2 KB  |  233 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: mgrmsgs.c,v 4.2 88/06/22 14:37:53 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/demo/misc/RCS/mgrmsgs.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/misc/RCS/mgrmsgs.c,v $$Revision: 4.2 $";
  12.  
  13. /* check for new messages */
  14.  
  15. #include <stdio.h>
  16. #include <signal.h>
  17. #include <sgtty.h>
  18. #include "term.h"
  19.  
  20. #define Isflag(arg,flag)    (!strncmp(arg,flag,strlen(flag)))
  21. #define Max(x,y)        ((x)>(y)?(x):(y))
  22. #define MENU_COUNT    (sizeof(menu)/sizeof(struct menu_entry))
  23. #define SCMP(x,y)    (strcmp(x+(strlen(x)-strlen(y)),y)==0)
  24.  
  25. #define RESUME    "Resume\n"
  26. #define MSGSCMD    "msgs -p;echo -n Done?\ "
  27. #define BOUNDS    "/usr/msgs/bounds"
  28. #define RC    ".msgsrc"
  29. #define POLL     60                /* polling interval */
  30. #define XPOS    220                /* start of msgs window */
  31. #define YPOS    170                /* start of msgs window */
  32.  
  33.  
  34. #define MSGS()    (1 + get_bounds(bounds) - get_rc(rc))
  35.  
  36. FILE *bounds, *rc;
  37. int msg_cnt, old_msg_cnt;
  38. char line[100];
  39. int poll=POLL;
  40.  
  41. struct menu_entry menu[] = {
  42.    "yes",    "y\r",
  43.    "skip",   "n\r",
  44.    "again",  "-\r",
  45.    "save",   "s\r",
  46.    "quit",   "q\r",
  47.    };
  48.  
  49. main(argc,argv)
  50. int argc;
  51. char **argv;
  52.    {
  53.    register int i;
  54.    int xpos = XPOS;
  55.    int ypos = YPOS;
  56.    int font = -1;
  57.    int shape = 1;
  58.  
  59.    char *getenv();
  60.    char *home = getenv("HOME");
  61.    int clean(), update();
  62.  
  63.    /* make sure we have a valid environment to run in */
  64.  
  65.    ckmgrterm( *argv );
  66.  
  67.    if (home==NULL || *home=='\0') {
  68.       fprintf(stderr,"%s: Can't find your home directory\n",argv[0]);
  69.       exit(1);
  70.       }
  71.  
  72.    if ((bounds = fopen(BOUNDS,"r")) == NULL) {
  73.       fprintf(stderr,"%s: Can't find a bounds file\n",argv[0]);
  74.       exit(2);
  75.       }
  76.  
  77.    sprintf(line,"%s/%s",home,RC);
  78.    
  79.    if ((rc = fopen(line,"r")) == NULL) {
  80.       fprintf(stderr,"%s: Can't find %s\n",argv[0],line);
  81.       exit(3);
  82.       }
  83.  
  84.    /* process arguments */
  85.  
  86.    for(i=1;i<argc;i++) {
  87.       if (Isflag(argv[i],"-s"))
  88.          shape = 0;
  89.       else if (Isflag(argv[i],"-x"))
  90.          xpos = atoi(argv[i]+2);
  91.       else if (Isflag(argv[i],"-y"))
  92.          ypos = atoi(argv[i]+2);
  93.       else if (Isflag(argv[i],"-f"))
  94.          font = atoi(argv[i]+2);
  95.       else if (Isflag(argv[i],"-p"))
  96.          poll  = Max(atoi(argv[i]+2),10);
  97.       else
  98.          usage(argv[0],argv[i]);
  99.       }
  100.  
  101.    /* setup mgr stuff */
  102.  
  103.    m_setup(M_FLUSH);
  104.    m_push(P_MENU|P_EVENT|P_FLAGS);
  105.    m_setmode(M_NOWRAP);
  106.    m_ttyset();
  107.  
  108.    signal(SIGTERM,clean);
  109.    signal(SIGINT,clean);
  110.    signal(SIGALRM,update);
  111.  
  112.    menu_load(1,MENU_COUNT,menu);
  113.    m_selectmenu(1);
  114.  
  115.    old_msg_cnt = MSGS();
  116.    get_msg(line,old_msg_cnt);
  117.    if (shape) {
  118.       m_setmode(M_ACTIVATE);
  119.       m_size(strlen(line)+2,1);
  120.       }
  121.    m_printstr(line);
  122.    m_setevent(REDRAW,"R\r");
  123.    m_setevent(ACTIVATED,"A\r");
  124.    m_clearmode(M_ACTIVATE);
  125.    alarm(poll);
  126.  
  127.    while(1) {
  128.       char buff[80];
  129.       m_gets(buff);
  130.       alarm(0);
  131.  
  132.       /* read msgs */
  133.  
  134.       old_msg_cnt = msg_cnt;
  135.       msg_cnt = MSGS();
  136.       if (msg_cnt > 0 && *buff == 'A') {
  137.          m_push(P_POSITION|P_EVENT|P_FLAGS|P_FONT);
  138.          if (font != -1)
  139.             m_font(font);
  140.          m_sizeall(xpos,ypos,80,24);
  141.          m_printstr("\freading msgs...\r");
  142.          m_ttyreset();
  143.          system(MSGSCMD);
  144.          m_gets(buff);
  145.          m_ttyset();
  146.          m_pop();
  147.          }
  148.  
  149.       /* wait for window to deactivate */
  150.  
  151.       else if (*buff == 'A') {
  152.          m_setevent(DEACTIVATED,RESUME);
  153.          do {
  154.             m_printstr("\f Your msgs system here        ");
  155.             m_gets(buff);
  156.             } while(!SCMP(buff,RESUME));
  157.          m_clearevent(DEACTIVATED);
  158.          }
  159.       old_msg_cnt = msg_cnt;
  160.       msg_cnt = MSGS();
  161.       get_msg(line,msg_cnt);
  162.       m_printstr(line);
  163.       m_clearmode(M_ACTIVATE);
  164.       alarm(poll);
  165.       }
  166.    }
  167.     
  168. int
  169. get_rc(file)
  170. FILE *file;
  171.    {
  172.    char line[100], *fgets();
  173.    fseek(file,0,0);
  174.    if (fgets(line,sizeof(line),file) != NULL) 
  175.       return(atoi(line));
  176.    else
  177.       return(0);
  178.    }
  179.  
  180. int
  181. get_bounds(file)
  182. FILE *file;
  183.    {
  184.    char buff[100], *line, *fgets();
  185.    fseek(file,0,0);
  186.    if ((line=fgets(buff,sizeof(buff),file)) != NULL) {
  187.       while(*line != ' ') line++;
  188.       while(*line == ' ') line++;
  189.       return(atoi(line));
  190.       }
  191.    else return(0);
  192.    }
  193.  
  194. get_msg(msg,cnt)
  195. int cnt;
  196. char *msg;
  197.    {
  198.    if (cnt > 0)
  199.       sprintf(msg,"\fYou have %d message%s waiting\r",cnt,cnt!=1?"s":"");
  200.    else if (cnt == 0)
  201.       sprintf(msg,"\fLooking for new messages\r");
  202.    else 
  203.       sprintf(msg,"\fMessage system scrunched\r");
  204.    }
  205.  
  206. clean()
  207.    {
  208.    m_ttyreset();
  209.    m_pop(0);
  210.    exit(1);
  211.    }
  212.  
  213. update()
  214.    {
  215.    msg_cnt = MSGS();
  216.    get_msg(line,msg_cnt);
  217.    if (msg_cnt != old_msg_cnt) {
  218.       if (msg_cnt > old_msg_cnt)         /* new messages */
  219.          m_printstr("\007");
  220.       m_printstr(line);
  221.       }
  222.    old_msg_cnt = msg_cnt;
  223.    alarm(poll);
  224.    }
  225.  
  226. usage(name,error)
  227. char *name, *error;
  228. {
  229.     fprintf(stderr,"Invalid flag: %s\n",error);
  230.     fprintf(stderr,"usage: %s -[s|x<pos>|y<pos>|f<font>|p<poll>\n",name);
  231.     exit(1);
  232. }
  233.